﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElementumForever.Decorators;
using Microsoft.Xna.Framework.Input;
using ElementumForever.Interfaces;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ElementumForever.Editer
{
    class SelectionManager : IMouseControl
    {
        List<BaseUIDecorator> _uiDecorators;
        public delegate void OnSelectEntityHandler(BaseEntity entity);
        public OnSelectEntityHandler OnSelectEntity = null;

        public SelectionManager()
        {
            _uiDecorators = new List<BaseUIDecorator>();
        }

        public void AddEntityDecorator(BaseDecorator decorator)
        {
            _uiDecorators.Add((BaseUIDecorator)decorator);
        }

        public void RemoveEntityDecorator(BaseDecorator decorator)
        {
            _uiDecorators.Remove((BaseUIDecorator)decorator);
        }

        //state xy is in scene local coordinate
        public void OnMouseClick(MouseState state)
        {
            bool select = false;
            foreach (BaseUIDecorator proxy in _uiDecorators)
            {
                proxy.OnMouseClick(state);
            }

            foreach (BaseUIDecorator proxy in _uiDecorators)
            {
                if (proxy.IsSelect)
                {
                    SetSelection(proxy.Component);
                    select = true;
                }
            }
            if (!select)
                SetSelection(null);
        }

        private void SetSelection(BaseEntity entity)
        {
            if (OnSelectEntity != null)
                OnSelectEntity(entity);
        }

        public void OnMouseDown(MouseState state)
        {
            foreach (BaseUIDecorator proxy in _uiDecorators)
                proxy.OnMouseDown(state);
        }

        public void OnMouseUp(MouseState state)
        {
            foreach (BaseUIDecorator proxy in _uiDecorators)
                proxy.OnMouseUp(state);
        }

        public void OnMouseMove(MouseState state)
        {
            foreach (BaseUIDecorator proxy in _uiDecorators)
                proxy.OnMouseMove(state);
        }

        public void OnDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            foreach (BaseUIDecorator proxy in _uiDecorators)
                proxy.OnDraw(gameTime, spriteBatch);
        }

        public void OnUpdate(GameTime gameTime)
        {
            foreach (BaseUIDecorator decorator in _uiDecorators)
                decorator.OnUpdate(gameTime);
        }
    }
}
